package capricornus.components.label
{
	import capricornus.components.utils.FilterUtils;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Label extends Sprite
	{
		public static const DEFAULT_TEXT_FORMAT:TextFormat = new TextFormat("_sans",12,0xffffff);
		public static const DEFAULT_TEXT_COLOR:uint = 0xffffff;
		
		protected var _underline:Boolean = false;
		
		protected var _container:Sprite;
		protected var _textField:TextField;
		
		protected var _horizontalCenter:Boolean = false;
		
		/**
		 * 如果是0，则认为要自动适应 
		 */		
		protected var _w:int = 0;
		protected var _h:int = 0;
		
		public function Label()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_container = new Sprite();
			addChild( _container );
			
			_textField = new TextField();
			_textField.defaultTextFormat = DEFAULT_TEXT_FORMAT;
			_textField.textColor = DEFAULT_TEXT_COLOR;
			_textField.multiline = true;
			_textField.wordWrap = false;
			_textField.filters = FilterUtils.getBlackGlowFilter();
			
			_container.addChild( _textField );
			
			_textField.width = 0;
			_textField.height = 0;
		}
		
		public function destroy():void
		{
			
		}
		
		/**
		 * 如果宽度设置成0，则认为是自动适应大小 
		 * @param w
		 * @param h
		 * 
		 */		
		public function setSize( w:int, h:int ):void
		{
			_w = w;
			_h = h;
			_textField.width = _w;
			_textField.height = _h;
			resize();
		}
		
		public function set defaultTextFormat( value:TextFormat ):void
		{
			if( _underline ) value.underline = _underline;
			
			_textField.defaultTextFormat = value;
		}
		
		public function set selectable( value:Boolean ):void
		{
			_textField.selectable = value;
		}
		
		public function set underline( value:Boolean ):void
		{
			if( _underline != value )
			{
				_underline = value;
				
				var tf:TextFormat = _textField.defaultTextFormat;
				tf.underline = value;
				_textField.defaultTextFormat = tf;
			}
		}
		
		public function set textColor( color:uint ):void
		{
			if( color != _textField.textColor ) _textField.textColor = color;
		}
		
		public function get textColor():uint
		{
			return _textField.textColor;
		}
		
		public function set text( value:String ):void
		{
			if( value==null ) return;
			_textField.text = value;
			resize();
		}
		
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set htmlText( value:String ):void
		{
			_textField.htmlText = value;
			resize();
		}
		
		public function get htmlText():String
		{
			return _textField.htmlText;
		}
		
		public function set horizontalCenter( value:Boolean ):void
		{
			if( _horizontalCenter != value )
			{
				_horizontalCenter = value;
				if( _horizontalCenter )
				{
					resize();
				}
				else
				{
					_textField.x = 0;
				}
			}
		}
		
		private function resize():void
		{
			if( _w==0 )
			{
				_textField.width = _textField.textWidth + 4;
				_textField.height = _textField.textHeight + 4;
			}
			if( _horizontalCenter )
			{
				_textField.x = -int(_textField.width/2);
			}
		}
	}
}